package ipdesk.model.report.invade.action;

import java.util.List;
import java.util.Map;

import ipdesk.model.businessInfo.bean.BusinessInfoBean;
import ipdesk.model.businessInfo.dao.BusinessInfoDao;
import ipdesk.model.common.dao.CommonDao;
import ipdesk.model.report.brand.dao.BrandInfoDao;
import ipdesk.model.report.design.dao.DesignInfoDao;
import ipdesk.model.report.invade.bean.InvadeInfoBean;
import ipdesk.model.report.invade.dao.InvadeInfoDao;
import ipdesk.model.report.seminar.dao.SeminarInfoDao;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import ecg.util.FileService;
import ecg.util.PageUtil;
import ecg.util.StringUtils;
import ecg.util.Time;

public class InvadeInfoSelectAction extends ActionSupport implements Preparable, ModelDriven<InvadeInfoBean>
{
	private InvadeInfoBean invadeInfoBean;
	private int totalCount;
	private Map pageInfoDataMap;
	private String deskCode;
	private List<InvadeInfoBean> invadeList;
	private InvadeInfoBean invadeInfoDetail;
	private InvadeInfoBean fileDetail;
	
	private BusinessInfoBean companyInfoDetail;
	private BusinessInfoBean customerInfoDetail;
	
	FileService fileService = new FileService();



	public String execute() throws Exception
	{
		
		totalCount = InvadeInfoDao.getInstance().getDataTotalCount(invadeInfoBean);
		
		pageInfoDataMap = PageUtil.getInstance().getPageInfoData(totalCount, invadeInfoBean.getPageNumber());
		
		if(invadeInfoBean.getDeskCode() != null || !"".equals(invadeInfoBean.getDeskCode())){
			
			pageInfoDataMap.put("deskCode", invadeInfoBean.getDeskCode());
			deskCode = invadeInfoBean.getDeskCode();
		}
		
		if(invadeInfoBean.getOrder() != null || !"".equals(invadeInfoBean.getOrder())){
			
			pageInfoDataMap.put("order", invadeInfoBean.getOrder());
		}
		
		String invadeSearchKeyword = invadeInfoBean.getInvadeSearchKeyword();
		
		if((invadeSearchKeyword != null) && (!invadeSearchKeyword.equals("")))
		{
			pageInfoDataMap.put("invadeSearchOption", invadeInfoBean.getInvadeSearchOption());
			pageInfoDataMap.put("invadeSearchKeyword", invadeInfoBean.getInvadeSearchKeyword());
		}
		
		if(invadeInfoBean.getYear() != null || !"".equals(invadeInfoBean.getYear())){
			
			pageInfoDataMap.put("year", invadeInfoBean.getYear());
		}
		
		if(invadeInfoBean.getState() != null || !"".equals(invadeInfoBean.getState())){
			
			pageInfoDataMap.put("state", invadeInfoBean.getState());
		}
		
		System.out.println("****데스크코드 : " + invadeInfoBean.getDeskCode());
		// 2. List
		invadeList = InvadeInfoDao.getInstance().getPagingDataListForUser(pageInfoDataMap);
		
		return SUCCESS;
	}
	
	public String executeDetail() throws Exception
	{
		int invadeSeq = invadeInfoBean.getInvadeSeq();
		//System.out.println("invadeSeq :: "+invadeSeq);
		
		invadeInfoDetail = InvadeInfoDao.getInstance().getDetailData(invadeSeq); 
		
		companyInfoDetail = BusinessInfoDao.getInstance().getDetailData(invadeInfoDetail.getCompanySeq());
		customerInfoDetail = BusinessInfoDao.getInstance().getModifyCustomerData(invadeInfoDetail.getCustomerSeq());
		
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeSeq, "FLE12");
		if(fileDetail != null) {
			invadeInfoDetail.setPath_name(fileDetail.getPath_name());
			invadeInfoDetail.setApplicationForm(fileDetail.getFile_name());
		}
		
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeSeq, "FLE31");
		if(fileDetail != null) {
			invadeInfoDetail.setPath_name(fileDetail.getPath_name());
			invadeInfoDetail.setEvidentialData(fileDetail.getFile_name());
		}
		
		return SUCCESS;
	}

	
	public String executeModifyForm() throws Exception
	{
		int invadeSeq = invadeInfoBean.getInvadeSeq();
		//System.out.println("invadeSeq :: "+invadeSeq);
		
		invadeInfoDetail = InvadeInfoDao.getInstance().getDetailData(invadeSeq);
		
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeSeq, "FLE12");
		if(fileDetail != null) {
			invadeInfoDetail.setPath_name(fileDetail.getPath_name());
			invadeInfoDetail.setApplicationForm(fileDetail.getFile_name());
			invadeInfoDetail.setFileSeq1(fileDetail.getFile_seq());
		}
		
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeSeq, "FLE31");
		if(fileDetail != null) {
			invadeInfoDetail.setPath_name(fileDetail.getPath_name());
			invadeInfoDetail.setEvidentialData(fileDetail.getFile_name());
			invadeInfoDetail.setFileSeq2(fileDetail.getFile_seq());
		}
		
		return SUCCESS;
	}
	
	public String executeModify() throws Exception
	{
		String returnFlag = "SUCCESS";
		System.out.println("***수정메소드 들어옴");
		// 데이터 업데이트, 파일 업로드 처리.
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.invade");
		String totalPath = uplodaPathPrefix + basePath;

		invadeInfoBean.setPath_name(basePath);
		
		String regTime = Time.getDateTime();
		invadeInfoBean.setRegTime(regTime);
		// desk 수정
		if(!invadeInfoBean.getDc1().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc1()); }
		if(!invadeInfoBean.getDc2().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc2()); }
		if(!invadeInfoBean.getDc3().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc3()); }
		if(!invadeInfoBean.getDc4().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc4()); }
		if(!invadeInfoBean.getDc5().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc5()); }
		if(!invadeInfoBean.getDc6().equals("")) { invadeInfoBean.setDeskCode(invadeInfoBean.getDc6()); }
		System.out.println("데스크코드 : " + invadeInfoBean.getDeskCode());
		

		
			InvadeInfoDao.getInstance().invadeInfoUpdate(invadeInfoBean);
			
			if(invadeInfoBean.getUpload() != null) {
				String uploadFileName = invadeInfoBean.getUploadFileName();
				String realFileName = fileService.saveFile(invadeInfoBean.getUpload(), totalPath, uploadFileName);
				invadeInfoBean.setUploadFileName(realFileName);
				invadeInfoBean.setFile_category("FLE12");
				
				InvadeInfoDao.getInstance().insertFileData(invadeInfoBean);
			}
			
			if(invadeInfoBean.getUploadED() != null) {
				String uploadFileName = invadeInfoBean.getUploadEDFileName();
				String realFileName = fileService.saveFile(invadeInfoBean.getUploadED(), totalPath, uploadFileName);
				invadeInfoBean.setUploadFileName(realFileName);
				invadeInfoBean.setFile_category("FLE31");
				
				InvadeInfoDao.getInstance().insertFileData(invadeInfoBean);
			}
			returnFlag = "SUCCESS";

		return returnFlag;
	}
	
	public String executeDelete() throws Exception
	{
		int invadeSeq = invadeInfoBean.getInvadeSeq();
	
		// 1. 해당 글의 모든 첨부파일 삭제
		deleteFilePath(invadeSeq, "FLE12");
		deleteFilePath(invadeSeq, "FLE31");
		deleteFilePath(invadeSeq, "FLE32");
		deleteFilePath(invadeSeq, "FLE33");
		
		// 2. 글 삭제
		InvadeInfoDao.getInstance().delDetailData(invadeSeq);	
		
		return SUCCESS;
	}
	
	public void deleteFilePath(int invadeSeq, String category) throws Exception
	{
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeSeq, category);		
		if(fileDetail != null) {
			String filePathPrefix = getText("path.upload.prefix");
			String fileName = fileDetail.getFile_name();
			String pathName = fileDetail.getPath_name();
									
			fileService.deleteFile(filePathPrefix + pathName + fileName);
									
			String fileSeq = fileDetail.getFile_seq();
			InvadeInfoDao.getInstance().delFileData(fileSeq);
		}
	}
	
	public String executeDeleteFile() throws Exception
	{
		String fileName="";
		System.out.println("**삭제메소드");
		if(invadeInfoBean.getFileSeq1() != null){
			invadeInfoBean.setFile_seq(invadeInfoBean.getFileSeq1());
			fileName = invadeInfoBean.getApplicationForm();
		} 
		
		if(invadeInfoBean.getFileSeq2() != null){
			invadeInfoBean.setFile_seq(invadeInfoBean.getFileSeq2());
			fileName = invadeInfoBean.getEvidentialData();
		} 
		
		if(invadeInfoBean.getFileSeq3() != null){
			invadeInfoBean.setFile_seq(invadeInfoBean.getFileSeq3());
			fileName = invadeInfoBean.getReport1();
		} 
		
		if(invadeInfoBean.getFileSeq4() != null){
			invadeInfoBean.setFile_seq(invadeInfoBean.getFileSeq4());
			fileName = invadeInfoBean.getReport2();
		}

		if(invadeInfoBean.getFile_seq() != null || !"".equals(invadeInfoBean.getFile_seq())){
			String fileSeq = invadeInfoBean.getFile_seq();
			
			// 1. 파일이 있다면, 파일 먼저 삭제
			String filePathPrefix = getText("path.upload.prefix");
			String pathName = invadeInfoBean.getPath_name();
			System.out.println("파일 경로는?" + filePathPrefix + pathName + fileName);
			fileService.deleteFile(filePathPrefix + pathName + fileName);
			
			// 2. file db에서 데이터 삭제			
			InvadeInfoDao.getInstance().delFileData(fileSeq);
		}
		
		return SUCCESS;
	}
	
	public String completeRegForm() throws Exception
	{
		invadeInfoDetail = InvadeInfoDao.getInstance().getDetailData(invadeInfoBean.getInvadeSeq());
		
		return SUCCESS;
	}
	
	public String completeReg() throws Exception
	{	
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.invade");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		invadeInfoBean.setPath_name(basePath);
		invadeInfoBean.setRegTime(regTime);

		InvadeInfoDao.getInstance().insertCompleteData(invadeInfoBean);
		
		if(invadeInfoBean.getUploadReport1() != null) {
			String uploadFileName = invadeInfoBean.getUploadReport1FileName();
			String realFileName = fileService.saveFile(invadeInfoBean.getUploadReport1(), totalPath, uploadFileName);
			invadeInfoBean.setUploadFileName(realFileName);
			invadeInfoBean.setFile_category("FLE32");

			InvadeInfoDao.getInstance().insertFileData(invadeInfoBean);
		}
		
		return SUCCESS;
	}
	
	public String completeDetail() throws Exception
	{
		invadeInfoDetail = InvadeInfoDao.getInstance().getDetailData(invadeInfoBean.getInvadeSeq()); 
		
		companyInfoDetail = BusinessInfoDao.getInstance().getDetailData(invadeInfoDetail.getAgentCompanySeq());
		customerInfoDetail = BusinessInfoDao.getInstance().getModifyCustomerData(invadeInfoDetail.getAgentCustomerSeq());
		
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeInfoBean.getInvadeSeq(), "FLE32");
		
		if(fileDetail != null) {
			invadeInfoDetail.setPath_name(fileDetail.getPath_name());
			invadeInfoDetail.setReport1(fileDetail.getFile_name());
			invadeInfoDetail.setFile_seq(fileDetail.getFile_seq());
		}

		return SUCCESS;
	}
	
	public String completeModifyForm() throws Exception
	{
		int invadeSeq = invadeInfoBean.getInvadeSeq();
		//System.out.println("invadeSeq :: "+invadeSeq);
		
		invadeInfoDetail = InvadeInfoDao.getInstance().getDetailData(invadeSeq);
		
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeSeq, "FLE32");
		if(fileDetail != null) {
			invadeInfoDetail.setPath_name(fileDetail.getPath_name());
			invadeInfoDetail.setReport1(fileDetail.getFile_name());
			invadeInfoDetail.setFileSeq3(fileDetail.getFile_seq());
		}
		
		return SUCCESS;
	}
	
	public String completeModify() throws Exception
	{
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.invade");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		invadeInfoBean.setPath_name(basePath);
		invadeInfoBean.setRegTime(regTime);
		
		InvadeInfoDao.getInstance().insertCompleteData(invadeInfoBean);
		
		if(invadeInfoBean.getUploadReport1() != null) {
			String uploadFileName = invadeInfoBean.getUploadReport1FileName();
			String realFileName = fileService.saveFile(invadeInfoBean.getUploadReport1(), totalPath, uploadFileName);
			invadeInfoBean.setUploadFileName(realFileName);
			invadeInfoBean.setFile_category("FLE32");
			
			InvadeInfoDao.getInstance().insertFileData(invadeInfoBean);
		}
		
		return SUCCESS;
	}
	
	public String adminRegForm() throws Exception
	{
		invadeInfoDetail = InvadeInfoDao.getInstance().getDetailData(invadeInfoBean.getInvadeSeq());
		
		return SUCCESS;
	}
	
	public String adminReg() throws Exception
	{
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.invade");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		invadeInfoBean.setPath_name(basePath);
		invadeInfoBean.setRegTime(regTime);

		InvadeInfoDao.getInstance().insertAdminData(invadeInfoBean);
		
		if(invadeInfoBean.getUploadReport2() != null) {
			String uploadFileName = invadeInfoBean.getUploadReport2FileName();
			String realFileName = fileService.saveFile(invadeInfoBean.getUploadReport2(), totalPath, uploadFileName);
			invadeInfoBean.setUploadFileName(realFileName);
			invadeInfoBean.setFile_category("FLE33");

			InvadeInfoDao.getInstance().insertFileData(invadeInfoBean);
		}
		
		return SUCCESS;
	}
	
	public String adminDetail() throws Exception 
	{
		invadeInfoDetail = InvadeInfoDao.getInstance().getDetailData(invadeInfoBean.getInvadeSeq()); 
		
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeInfoBean.getInvadeSeq(), "FLE33");
		
		if(fileDetail != null) {
			invadeInfoDetail.setPath_name(fileDetail.getPath_name());
			invadeInfoDetail.setReport2(fileDetail.getFile_name());
			invadeInfoDetail.setFile_seq(fileDetail.getFile_seq());
		}
		
		return SUCCESS;
	}
	
	public String adminModifyForm() throws Exception
	{
		int invadeSeq = invadeInfoBean.getInvadeSeq();
		//System.out.println("invadeSeq :: "+invadeSeq);
		
		invadeInfoDetail = InvadeInfoDao.getInstance().getDetailData(invadeSeq);
		
		fileDetail = InvadeInfoDao.getInstance().getFileDetailData(invadeSeq, "FLE33");
		if(fileDetail != null) {
			invadeInfoDetail.setPath_name(fileDetail.getPath_name());
			invadeInfoDetail.setReport2(fileDetail.getFile_name());
			invadeInfoDetail.setFileSeq4(fileDetail.getFile_seq());
		}
		
		return SUCCESS;
	}
	
	public String adminModify() throws Exception
	{
		String uplodaPathPrefix = getText("path.upload.prefix");
		String basePath = getText("path.upload.invade");
		String totalPath = uplodaPathPrefix + basePath;
		
		String regTime = Time.getDateTime();
		
		invadeInfoBean.setPath_name(basePath);
		invadeInfoBean.setRegTime(regTime);
		
		InvadeInfoDao.getInstance().insertAdminData(invadeInfoBean);
		
		if(invadeInfoBean.getUploadReport2() != null) {
			String uploadFileName = invadeInfoBean.getUploadReport2FileName();
			String realFileName = fileService.saveFile(invadeInfoBean.getUploadReport2(), totalPath, uploadFileName);
			invadeInfoBean.setUploadFileName(realFileName);
			invadeInfoBean.setFile_category("FLE33");
			
			InvadeInfoDao.getInstance().insertFileData(invadeInfoBean);
		}
		
		return SUCCESS;
	}
	
	public void prepare() throws Exception {
		
		invadeInfoBean = new InvadeInfoBean();
	}

	public InvadeInfoBean getModel() {

		return invadeInfoBean;
	}
	
	public Map getPageInfoDataMap()
	{
		return pageInfoDataMap;
	}

	public void setPageInfoDataMap(Map pageInfoDataMap)
	{
		this.pageInfoDataMap = pageInfoDataMap;
	}
	
	public List<InvadeInfoBean> getInvadeList() {
		return invadeList;
	}

	public void setInvadeList(List<InvadeInfoBean> invadeList) {
		this.invadeList = invadeList;
	}
	
	public String getDeskCode() {
		return deskCode;
	}

	public int getTotalCount() {
		return totalCount;
	}
	
	public InvadeInfoBean getInvadeInfoDetail() {
		return invadeInfoDetail;
	}

	public InvadeInfoBean getFileDetail() {
		return fileDetail;
	}

	public BusinessInfoBean getCompanyInfoDetail() {
		return companyInfoDetail;
	}

	public BusinessInfoBean getCustomerInfoDetail() {
		return customerInfoDetail;
	}


}


